package com.gcxy.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gcxy.entity.Agency;
import com.gcxy.service.AgencyService;
import com.gcxy.service.EvaluateService;
import com.gcxy.service.GuidanceService;
import com.gcxy.service.WeeklyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName ScheduledTasks
 * @Author qinlun
 * @Date 2024/7/29 11:05
 * @Describe 定时任务调度
 **/
@Slf4j
@Component
public class ScheduledTasks {

    @Autowired
    private WeeklyService weeklyService;

    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private GuidanceService guidanceService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private AgencyService agencyService;


    /**
     * 每周定时每周五0点新增周报
     */
    @Scheduled(cron = "0 0 0 * * 5")
    public void addWeeklyEvaluate() {
        log.info("开始执行定时任务：新增周报和周评价信息");
        List<Agency> agencyList = agencyService.getHasWeeklyAgencyList();
        if (CollectionUtils.isNotEmpty(agencyList)) {
            agencyList.forEach(agency -> {
                try {
                    weeklyService.addWeekly(agency.getId());
                    evaluateService.addEvaluate(agency.getId());
                } catch (Exception e) {
                    log.error("执行任务失败：", e);
                }
            });
        } else {
            log.info("当前时间内没有需要新增的周报信息");
            log.info("当前时间内没有需要新增的周评价信息");
        }
    }

    /**
     * 每周定时每周六23：50：00点新增周指导记录
     */
    @Scheduled(cron = "0 50 23 * * 6")
    public void addGuidance() {
        log.info("开始执行定时任务：新增周指导记录信息");
        List<Agency> agencyList = agencyService.getHasWeeklyAgencyList();
        if (CollectionUtils.isNotEmpty(agencyList)) {
            agencyList.forEach(agency -> {
                try {
                    guidanceService.addGuidance(agency.getId());
                } catch (Exception e) {
                    log.error("新增周指导记录失败", e);
                }
            });
        } else {
            log.info("当前时间内没有需要新增的周指导记录");
        }
    }

}
